package com.test.designPattern.ch12.shape;

import java.util.HashMap;

/**
 * @ClassName ShapeFactory
 * @Description 享元模式（Flyweight Pattern）主要用于减少创建对象的数量，
 *              以减少内存占用和提高性能。大量创建对象时，通过HashMap缓存对象，避免大量重复创建
 * @Author 维康
 * @Date 2018/6/11 14:47
 * @Version 1.0
 **/
public class ShapeFactory {

    private static final HashMap<String, Shape> circleMap = new HashMap<>();

    public static Shape getCircle(String color) {
        Circle circle = (Circle)circleMap.get(color);

        if(circle == null) {
            circle = new Circle(color);
            circleMap.put(color, circle);
            System.out.println("Creating circle of color : " + color);
        }
        return circle;
    }
}
